package com.yyy.dm.consumer.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yyy.dm.CommonResponse;
import com.yyy.dm.consumer.service.UserConsumerService;
import com.yyy.dm.user.UserInfoDTO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserConsumerController {

    @Resource
    private UserConsumerService userConsumerService;
    //获取登录短信
    @PostMapping(path = "/msg")
    public CommonResponse<String> msg(@RequestBody Map<String,String> param){
        String phoneNumber = param.get("phone");
        return userConsumerService.sendSMS(phoneNumber);
    }
    //短信登录
    @PostMapping("/msg/login")
    public CommonResponse smsLogin(@RequestBody Map<String,String> map){
        String phone = map.get("phone");
        String vcode = map.get("vcode");
        return userConsumerService.smsLogin(phone,vcode);
    }

    //获取注册短信
    @PostMapping(path = "/code")
    public CommonResponse<String> code(@RequestBody Map<String,String> param){
        String phoneNumber = param.get("phone");
        return userConsumerService.code(phoneNumber);
    }
    //注册



    @PostMapping("/register")
    public CommonResponse register(@RequestBody Map<String,String> map){
        String phone = map.get("phone");
        String password = map.get("password");
        String vcode = map.get("vcode");
        return userConsumerService.register(phone,password,vcode);
    }

    //短信登录
    @PostMapping("/login")
    public CommonResponse login(@RequestBody Map<String,String> map){
        String phone = map.get("phone");
        String password = map.get("password");
        return userConsumerService.login(phone,password);
    }

    //获取用户信息
    @PostMapping("/permission/querypersoninfo")
    public CommonResponse queryPersonInfo(@RequestBody Map<String,String> map){
        Long userId = Long.parseLong(map.get("userId"));
        return userConsumerService.queryPersonInfo(userId);
    }

    //修改用户信息
    @PostMapping("/permission/modifypersoninfo")
    public CommonResponse modifyPersonInfo(@RequestBody Map<String,String> map, HttpServletResponse response){
        UserInfoDTO userInfoDTO = new ObjectMapper().convertValue(map,UserInfoDTO.class);
        return userConsumerService.modifyPersonInfo(userInfoDTO,response);
    }


}
